// pages/category/category.js
const app = getApp();
import {
  list
} from '../../../api/product'

import {
  add,
  buyNow
} from '../../../api/cart'

import {
  productCategoryList
} from '../../../api/productCategory'
const util = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    categoryId: null,
    pageNum: 1,
    pageSize: 20,
    total: 0,
    products: [],
    categoryName: "", //记录当前滚动的距离
    eInfo: "", //暂存购物车动画的e数据
    showNoMore: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    this.data.categoryId = options.id
    this.getProductrequest(options.id)
  },

  detailsClick(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goods/product/details?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let statusBarHeight = app.globalData.statusBarHeight
    this.setData({
      statusBarHeight: statusBarHeight
    })
  },

  //跳转搜索页
  categorySearch() {
    wx.navigateTo({
      url: '/pages/goods/search/index',
    })
  },

  //获取顶部的高度
  getFirstTop() {
    //选择id
    var _this = this;
    let query = wx.createSelectorQuery()
    query.select('.category-search').boundingClientRect()
    query.select('.addCart').boundingClientRect()
    query.select('.category-firstLevel').boundingClientRect()
    query.exec((res) => {
      _this.setData({
        firstTop: res[0].bottom,
        firstHeight: res[1].top - res[0].bottom - 10
      })
    })
  },

  //获取商品列表信息
  getProductrequest(id) {
    const that = this
    let { pageSize, pageNum } = that.data
    list({
      pageNum: pageNum,
      pageSize: pageSize,
      mchId: app.globalData.mchId,
      productCategoryId: id,
      sortField: "product_category_id"
    }).then(res => {
      if (res.type == 'success') {
        res.data.data.map(el => el.thumbnail = util.thumbnail(el.thumbnail, 350, 350))
        that.setData({
          categoryName: res.data.data[0].productCategoryName,
          products: [...that.data.products, ...res.data.data],
          total: res.data.total
        })
        wx.stopPullDownRefresh()
      }
    }
    ).catch(error => {
    })
  },
  //点击购物车事件
  addProductToCart(e) {
    if (util.isNull(app.globalData.token)) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return
    }
    let productId = e.currentTarget.dataset.productinfo.id
    this.data.eInfo = e
    if (e.currentTarget.dataset.productinfo.sku > 1) {
      this.selectComponent('#bottomFrame').showFrame(productId, "addCart");
    } else {
      //发送添加购物车的请求
      let data = {}
      data.productId = productId
      data.promotionId = ""
      data.quantity = e.currentTarget.dataset.productinfo.minLimit
      data.uuid = app.globalData.uuid
      add(data).then(
        res => {
          if (res.type == 'success') {
            wx.showToast({
              title: '成功加入购物车',
              duration: 2000
            })
            this.selectedInfo()
          }
        }
      ).catch(error => {

      })
    }
  },

  //属性选择弹窗完成后数据回传函数
  selectedInfo(data) {
    this.selectComponent('#addCart').touchOnGoods(this.data.eInfo);
  },
  onPullDownRefresh() {
    this.setData({
      pageNum: 1,
      total: 0,
      products: []
    })
    this.getProductrequest(this.data.categoryId)
  },
  //上啦加载
  onReachBottom(e) {
    let { pageSize, pageNum, total, categoryId } = this.data
    if (pageSize * pageNum >= total) {
      this.setData({
        showNoMore: true
      })
      return;
    }
    this.data.pageNum++;
    this.getProductrequest(categoryId)
  },
})